package com.crawler;

import com.core.interceptor.RenderJspInterceptor;
import com.core.plugin.bind.AutoRouteBindPlugin;
import com.core.plugin.bind.AutoTableBindPlugin;
import com.core.plugin.bind.TableNameStyle;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.ext.handler.FakeStaticHandler;
import com.jfinal.plugin.activerecord.SqlReporter;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.render.ViewType;

/**
 * API引导式配置
 */
public class RunConfig extends JFinalConfig {
	
	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {
		loadPropertyFile("jdbc.properties");
		me.setDevMode(getPropertyToBoolean("devMode", false));
		me.setViewType(ViewType.JSP); 
		SqlReporter.setLogger(true);
	}
	
	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		AutoRouteBindPlugin.add(me);
	}
	
	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
		C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password").trim());
		me.add(c3p0Plugin);
        //数据库表和实体自动绑定
        AutoTableBindPlugin autoTableBindPlugin = new AutoTableBindPlugin(c3p0Plugin, TableNameStyle.LOWER);
		autoTableBindPlugin.setShowSql(true);
        me.add(autoTableBindPlugin);
        //初始化sql语句
//       me.add(new SqlInXmlPlugin());
        //初始化缓存插件
//		me.add(new EhCachePlugin());
		//初始化定时调度插件
//		me.add(new QuartzPlugin());
	}
	
	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
		me.add(new RenderJspInterceptor());
	}
	
	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {
		me.add(new ContextPathHandler("contextPath"));
		me.add(new FakeStaticHandler(".jhtml"));
	}
	
	/**
	 * 建议使用 JFinal 手册推荐的方式启动项目
	 * 运行此 main 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
	 */
	public static void main(String[] args) {
		JFinal.start("WebRoot", 80, "/", 5);
	}

	@Override
	public void afterJFinalStart() {
		
	}
}
